package com.farmer.training.controller;

import com.farmer.common.entity.TrainingCourse;
import com.farmer.common.result.Result;
import com.farmer.training.service.TrainingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 培训课程控制器
 */
@Slf4j
@RestController
@RequestMapping("/training")
@Tag(name = "培训课程管理", description = "培训课程相关接口")
public class TrainingController {

    @Autowired
    private TrainingService trainingService;

    /**
     * 获取培训课程列表（分页）
     */
    @GetMapping("/list")
    @Operation(summary = "获取培训课程列表", description = "分页获取培训课程列表，支持条件筛选")
    public Result<?> getCourseList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "课程分类") @RequestParam(required = false) String category,
            @Parameter(description = "课程类型") @RequestParam(required = false) String courseType,
            @Parameter(description = "课程状态") @RequestParam(required = false) String status,
            @Parameter(description = "搜索关键词") @RequestParam(required = false) String keyword) {
        return trainingService.getCourseList(pageNum, pageSize, category, courseType, status, keyword);
    }

    /**
     * 根据ID获取课程详情
     */
    @GetMapping("/{courseId}")
    @Operation(summary = "获取课程详情", description = "根据课程ID获取详细信息")
    public Result<?> getCourseById(@Parameter(description = "课程ID") @PathVariable Long courseId) {
        return trainingService.getCourseById(courseId);
    }

    /**
     * 添加培训课程
     */
    @PostMapping
    @Operation(summary = "添加培训课程", description = "创建新的培训课程")
    public Result<?> addCourse(@RequestBody TrainingCourse course) {
        return trainingService.addCourse(course);
    }

    /**
     * 更新课程信息
     */
    @PutMapping
    @Operation(summary = "更新课程信息", description = "更新现有培训课程信息")
    public Result<?> updateCourse(@RequestBody TrainingCourse course) {
        return trainingService.updateCourse(course);
    }

    /**
     * 删除课程
     */
    @DeleteMapping("/{courseId}")
    @Operation(summary = "删除课程", description = "删除指定的培训课程")
    public Result<?> deleteCourse(@Parameter(description = "课程ID") @PathVariable Long courseId) {
        return trainingService.deleteCourse(courseId);
    }

    /**
     * 更新课程状态
     */
    @PutMapping("/{courseId}/status")
    @Operation(summary = "更新课程状态", description = "更新课程发布状态")
    public Result<?> updateCourseStatus(
            @Parameter(description = "课程ID") @PathVariable Long courseId,
            @Parameter(description = "新状态") @RequestParam String status) {
        return trainingService.updateCourseStatus(courseId, status);
    }

    /**
     * 获取推荐课程列表
     */
    @GetMapping("/recommended")
    @Operation(summary = "获取推荐课程", description = "获取推荐的培训课程列表")
    public Result<?> getRecommendedCourses(
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "10") Integer limit) {
        return trainingService.getRecommendedCourses(limit);
    }

    /**
     * 获取课程分类列表
     */
    @GetMapping("/categories")
    @Operation(summary = "获取课程分类", description = "获取所有课程分类列表")
    public Result<?> getCategoryList() {
        return trainingService.getCategoryList();
    }

    /**
     * 获取讲师的课程列表
     */
    @GetMapping("/instructor/{instructorId}")
    @Operation(summary = "获取讲师课程", description = "获取指定讲师的课程列表")
    public Result<?> getCoursesByInstructor(
            @Parameter(description = "讲师ID") @PathVariable Long instructorId,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        return trainingService.getCoursesByInstructor(instructorId, pageNum, pageSize);
    }

    /**
     * 增加报名人数
     */
    @PutMapping("/{courseId}/enrollment/increase")
    @Operation(summary = "增加报名人数", description = "增加课程报名人数")
    public Result<?> increaseEnrollmentCount(@Parameter(description = "课程ID") @PathVariable Long courseId) {
        return trainingService.increaseEnrollmentCount(courseId);
    }

    /**
     * 减少报名人数
     */
    @PutMapping("/{courseId}/enrollment/decrease")
    @Operation(summary = "减少报名人数", description = "减少课程报名人数")
    public Result<?> decreaseEnrollmentCount(@Parameter(description = "课程ID") @PathVariable Long courseId) {
        return trainingService.decreaseEnrollmentCount(courseId);
    }

    /**
     * 获取课程统计信息
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取课程统计", description = "获取培训课程相关统计信息")
    public Result<?> getCourseStatistics() {
        return trainingService.getCourseStatistics();
    }
} 